﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace AutoDalModel
{
    public partial class frmMain : Form
    {        
        DatabaseInfo di;

        public frmMain()
        {
            InitializeComponent();
            di = new DatabaseInfo();
        }

        private bool CheckServerInput()
        {
            if (textServer.Text.Trim() == "")
            {
                MessageBox.Show("请输入服务器IP和端口号。");
                textServer.Focus();
                return false;
            }
            if ((radioMgr.Checked) && ((textUID.Text.Trim() == "") || (textPWD.Text.Trim() == "")))
            {
                MessageBox.Show("请输入数据库服务器管理员账号。");
                textUID.Focus();
                return false;
            }
            return true;
        }

        private bool CheckInput()
        {
            if (!CheckServerInput())
                return false;
            if (cmbDatabase.SelectedIndex < 0) 
            {
                MessageBox.Show("请选择一个数据库。");
                cmbDatabase.Focus();
                return false;
            }
            if (textSpace.Text.Trim() == "")
            {
                MessageBox.Show("请输入命名空间。");
                textSpace.Focus();
                return false;
            }
            if (textPath.Text.Trim() == "")
            {
                MessageBox.Show("请选择导出目录。");
                textPath.Focus();
                return false;
            }
            return true;
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //pnlUID.Enabled = rgRz.SelectedIndex == 1;
        }

        private void bbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            if(!CheckInput())
                return;
            di.myData.DatabaseName = cmbDatabase.Text;
            try
            {
                di.MakeModel(textSpace.Text, cmbModel.Text, textPath.Text);
                MessageBox.Show("生成模型类成功！");
            }
            catch(Exception E)
            {
                MessageBox.Show(E.Message);
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (!CheckServerInput())
                return;
            di.myData.ServerName = textServer.Text;            
            di.myData.IsWindow = radioWin.Checked;
            di.myData.UID = textUID.Text;
            di.myData.PWD = textPWD.Text;
            cmbDatabase.SelectedIndex = -1;
            cmbDatabase.Items.Clear();
            if (di.TestServer())
            {
                MessageBox.Show("连接数据库成功，请选择一个数据库！");
                List<Model.DatabaseModel> dl = di.myData.GetDatabaseList();
                for (int i = 0; i < dl.Count; i++)
                {
                    cmbDatabase.Items.Add(dl[i].DatabaseName); ;
                }
            }
            else
            {                
                MessageBox.Show("连接数据库失败！");
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.textServer.Focus();
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = this.textPath.Text;
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textPath.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void lLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.programbbs.com");
        }
    }
}